<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    chartData: {
      type: Object,
      required: true
    },
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
function randomData() {
    now = new Date(+now + oneDay);
    value = value + Math.random() * 20 - 10;
    return {
        name: now.toString(),
        value: [
            [now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'),
            Math.round(value)
        ]
    };
}
var data = [];
var now = +new Date(2020, 1, 9);
var oneDay = 24 * 3600 * 1000;
var value = Math.random() * 1000;

for (var i = 0; i < 50; i++) {
    data.push(randomData());
}
      this.chart.setOption({
        title: {
          text: this.chartData.resourceType
        },
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
              params = params[0];
              var date = new Date(params.name);
              return date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' : ' + params.value[1];
          },
          axisPointer: {
              animation: false
          }
        },
        xAxis: {
          type: 'time',
          splitLine: {
              show: false
          }
        },
        yAxis: {
          type: 'value',
          boundaryGap: [0, '100%'],
          splitLine: {
              show: false
          }
        },
        series: [{
          name: this.chartData.resourceType+'实时数据',
          type: 'line',
          showSymbol: false,
          hoverAnimation: false,
          data: data,
          areaStyle: {}
        }]
      })
    
    }
  }
}
</script>
